<?php
namespace Act\Os;
use Lib\ErrorMessage;
use Lib\Common;
use Lib\Page;
class Users extends Backend{
	public function doGet(){
		$user_group = get('user_group',0,'absint');
		$method = get('method');
		$user_name = get('user_name','','htmlspecialchars');
		$user_id = get('user_id',0,'absint');
		$user_mod = model('#User','adm_');
		$count = $user_mod->searchCount($user_group,$user_name);
		$page = new Page($count,self::PAGE_SIZE);

		$user_infos = $user_mod->search($user_group ,$user_name, $start = $page->offset ,$page_size=$page->size);

		$group_options = model('#UserGroup','adm_')->getGroupForOptions();
		$group_options[0] = '全部';
		$this->display('index.php',array(
			'group_options' => $group_options,
			'user_infos' => $user_infos,
			'page_no' => $page->page_no,
			'page_html' => $page->show(),
			'admin_action_confirm'  => Common::renderJsConfirm("icon-pause,icon-play,icon-remove")
		));
	}

	public function doUpdate(){
		$user_id = get('user_id',0,'absint');
		$this->checkParam($user_id);
		switch(get('method','')){
			case 'pause':
				return $this->doPause($user_id,model('#User','adm_'));
				break;
			case 'play':
				return $this->doPlay($user_id,model('#User','adm_'));
				break;
			default:
				return $this->error('非法操作','/os/users');
				break;
		}
	}

	private function doPause($user_id,$user_mod){
		$user_data=array("status"=>0);
		$user_info = session('user_info');
		if($user_id == $user_info['user_id']){
			return $this->error(ErrorMessage::CAN_NOT_DO_SELF,'os/users');
		}else{
			if($user_id==1){
				$this->success ( '不能封停初始管理员','os/users' );
			}
			$result = $user_mod->updateUser ( $user_id,$user_data );
			if ($result>=0) {
				model('#SysLog','adm_')->addLog ( $user_info['user_name'], 'PAUSE',  'User' ,$user_id ,json_encode($user_data) );
				$this->success ( '已封停','os/users' );
			}else{
				$this->error("error");
			}
		}
	}

	private function doPlay($user_id,$user_mod){
		$user_data=array("status"=>1);
		$result = $user_mod->updateUser ( $user_id,$user_data );
		if ($result>=0) {
			model('#SysLog','adm_')->addLog ( session('user_info')['user_name'], 'PLAY' , 'User' ,$user_id ,json_encode($user_data) );
			$this->success('已解封','os/users');
		}else{
			$this->error("error");
		}
	}
}